package com.hschinese.pronunciation.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;


import com.hschinese.pronunciation.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 
 * @author Administrator 其中使用的format为日期字符串的格式时间表现 如日期格式为 201312091324 format为
 *         yyyyMMddHHmmss 2013-04-30 23:23:23 format为yyyy-MM-dd HH:mm:ss
 */
public class DateConvert {
	private String format = "yyyy-MM-dd HH:mm";
	public static Date oldDate = null;
	// private static String YEAR_FORMAT = MyApplication.getInstance().getString(R.string.year_format);

	// 记录开始计算日期 显示日期
	// 判断时间 如果发送信息之间的时间差为8分钟 就插入一条时间提示
	public boolean insertTime() {
		if (null == oldDate) {
			oldDate = new Date();
			return true;
		} else if (intervalMinutes(oldDate) >= 8) {
			oldDate = new Date();
			return true;
		}
		return false;
	}

	// 获取间隔分钟
	public int intervalMinutes(Date oldDate) {
		long end = oldDate.getTime();
		long current = new Date().getTime();
		return (int) (current - end) / (1000 * 60);
	}

	// eDate 发送时间 cDate 当前时间 获取date和当前时间相隔几天
	public int intervalDays(String date) {
		int days = 0;
		try {
			date = date.replaceAll("-", "").replaceAll(":", "").trim();
			Date eDate = StringToDate(date);
			Calendar endDate = Calendar.getInstance();
			endDate.setTime(eDate);
			Date cDate = new Date();
			Calendar currentDate = Calendar.getInstance();
			currentDate.setTime(cDate);
			if (endDate.after(currentDate)) {
				Calendar swap = endDate;
				endDate = currentDate;
				currentDate = swap;
			}
			days = currentDate.get(Calendar.DAY_OF_YEAR)
					- endDate.get(Calendar.DAY_OF_YEAR);
			int y1 = endDate.get(Calendar.YEAR);
			int y2 = currentDate.get(Calendar.YEAR);
			if (y1 != y2) {
				endDate.clone();
				do {
					days += endDate.getActualMaximum(Calendar.DAY_OF_YEAR);
					endDate.add(Calendar.YEAR, 1);
				} while (y1 != y2);
			}
		} catch (Exception e) {
			// TODO: handle exception
			days = 0;
		}
		return days;
	}
	
	/**
	 * 获取两个日期之间的间隔天数
	 * @return
	 */
	public int getGapCount(Date startDate, Date endDate) {
		Calendar fromCalendar = Calendar.getInstance();
		fromCalendar.setTime(startDate);  
		fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
		fromCalendar.set(Calendar.MINUTE, 0);
		fromCalendar.set(Calendar.SECOND, 0);
		fromCalendar.set(Calendar.MILLISECOND, 0);

		Calendar toCalendar = Calendar.getInstance();
		toCalendar.setTime(endDate);
		toCalendar.set(Calendar.HOUR_OF_DAY, 0);
		toCalendar.set(Calendar.MINUTE, 0);
		toCalendar.set(Calendar.SECOND, 0);
		toCalendar.set(Calendar.MILLISECOND, 0);
		return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
	}

	// 把数字转换成中文字符(周)
	public String getWeek(int week) {
		int detailWeek = week - 1;
		String result = "";
		switch (detailWeek) {
		case 0:
			result = "日";
			break;
		case 1:
			result = "一";
			break;
		case 2:
			result = "二";
			break;
		case 3:
			result = "三";
			break;
		case 4:
			result = "四";
			break;
		case 5:
			result = "五";
			break;
		case 6:
			result = "六";
			break;
		default:
			break;
		}
		return "周" + result;
	}

	// 直接转换成字符串
	public String getWeekDetails(String date) {
		String result = "";
		try {
			if (!"".equals(date)) {
				Date resultDate = StringToDate(date);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(resultDate);
				int week = calendar.get(Calendar.DAY_OF_WEEK);
				result = getWeek(week);
			} else {
				result = "";
			}
		} catch (Exception e) {
			// TODO: handle exception
			result = "";
		}
		return result;
	}

	// 获取时间段 分为5个时段 凌晨 24-6 早上 7-9 (上午 9-12//已去掉) 下午 13-18 晚上 18-24
	public String getTimeBucket(String date) {
		String[] results = new String[] { "凌晨 ", "早上", "下午", "晚上" };
		String result = "";
		try {
			Date resultDate = StringToDate(date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(resultDate);
			int hour = calendar.get(Calendar.HOUR_OF_DAY);
			int index = hour / 6;
			result = results[index];
		} catch (Exception e) {
			// TODO: handle exception
			result = "";
		}
		return result;
	}

	public String getActivityTimeDetails(String date) {
		String result = "";
		try {
			if (!"".equals(date)) {
				Date resultDate = StringToDate(date);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(resultDate);
				String suffix = "";
				int hour = calendar.get(Calendar.HOUR_OF_DAY);
				int minute = calendar.get(Calendar.MINUTE);
				if (Calendar.AM == calendar.get(Calendar.AM_PM)) {
					suffix = "AM";
				} else {
					suffix = "PM";
				}
				String h = "";
				if (hour < 10) {
					h = String.valueOf("0" + hour);
				} else {
					h = String.valueOf(hour);
				}
				String m = "";
				if (minute < 10) {
					m = String.valueOf("0" + minute);
				} else {
					m = String.valueOf(minute);
				}
				result = h + ":" + m + suffix;
			}
		} catch (Exception e) {
			// TODO: handle exception
			result = "";
		}

		return result;
	}

	// 获取小时和分钟
	public String getTimeDetails(String date) {
		String result = "";
		try {
			if (!"".equals(date)) {
				Date resultDate = StringToDate(date);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(resultDate);
				int hour = calendar.get(Calendar.HOUR_OF_DAY);
				int minute = calendar.get(Calendar.MINUTE);
				String h = "";
				if (hour < 10) {
					h = String.valueOf("0" + hour);
				} else {
					h = String.valueOf(hour);
				}
				String m = "";
				if (minute < 10) {
					m = String.valueOf("0" + minute);
				} else {
					m = String.valueOf(minute);
				}
				result = h + ":" + m;
			}
		} catch (Exception e) {
			// TODO: handle exception
			result = "";
		}

		return result;
	}


	// 获取小时和分钟
	public String getTimeDetails(Date resultDate) {
		String result = "";
		try {
			if (null != resultDate) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(resultDate);
				int hour = calendar.get(Calendar.HOUR_OF_DAY);
				int minute = calendar.get(Calendar.MINUTE);
				String h = "";
				if (hour < 10) {
					h = String.valueOf("0" + hour);
				} else {
					h = String.valueOf(hour);
				}
				String m = "";
				if (minute < 10) {
					m = String.valueOf("0" + minute);
				} else {
					m = String.valueOf(minute);
				}
				result = h + ":" + m;
			}
		} catch (Exception e) {
			// TODO: handle exception
			result = "";
		}

		return result;
	}

	// 获取号
	public String getMarkDetails(String date) {
		String result = "";
		try {
			if (!"".equals(date)) {
				Date resultDate = StringToDate(date);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(resultDate);
				int day = calendar.get(Calendar.DAY_OF_MONTH);
				if (day < 10) {
					result = String.valueOf("0" + day);
				} else {
					result = String.valueOf(day);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			result = "";
		}

		return result;
	}

	@SuppressLint("SimpleDateFormat")
	public String getMarkDetailsByDate(Date date){
		String result = "";
		if(null != date){
			result = new SimpleDateFormat("MM-dd").format(date);
		}
		return result;
	}

	@SuppressLint("SimpleDateFormat")
	public String getDetailByDate(Date date){
		String result = "";
		if(null != date){
			// result = new SimpleDateFormat(YEAR_FORMAT).format(date);
		}
		return result;
	}

	// 获取月
	public String getMonthChina(String date) {
		String result = "";
		String mon = getMonthDetails(date).replace("月", "");
		if (!TextUtils.isEmpty(mon)) {
			int month = Integer.valueOf(mon);
			switch (month) {
			case 1:
				result = "一月";
				break;
			case 2:
				result = "二月";
				break;
			case 3:
				result = "三月";
				break;
			case 4:
				result = "四月";
				break;
			case 5:
				result = "五月";
				break;
			case 6:
				result = "六月";
				break;
			case 7:
				result = "七月";
				break;
			case 8:
				result = "八月";
				break;
			case 9:
				result = "九月";
				break;
			case 10:
				result = "十月";
				break;
			case 11:
				result = "十一月";
				break;
			case 12:
				result = "十二月";
				break;
			default:
				break;
			}
		} else {
			result = "";
		}
		return result;
	}

	// 获取月的详情 小于10前面加0
	public String getMonthDetails(String date) {
		String result = "";
		try {
			if (!"".equals(date)) {
				Date resultDate = StringToDate(date);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(resultDate);
				int month = calendar.get(Calendar.MONTH) + 1;
				if (month < 10) {
					result = String.valueOf("0" + month + "月");
				} else {
					result = String.valueOf(month + "月");
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			result = "";
		}

		return result;
	}

	/**
	 * 字符串转换成date类型
	 * 
	 * @param stringDate
	 *            需要转换的字符串
	 * @param format
	 *            转换的字符串格式
	 * @return
	 */

	// 把String转换为date
	@SuppressLint("SimpleDateFormat")
	public Date StringToDate(String stringDate) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(stringDate);
		} catch (ParseException e) {
			e.printStackTrace();
			date = null;
		}
		return date;
	}

	// 外国 一般周日为一周的开始 判断是否为同一周时使用
	public boolean isSampleWeek(String date) {
		String week = getWeekDetails(date);
		boolean result = SampleWeek(date);
		if ("周日".equals(week)) {
			if (result) {
				result = false;
			} else {
				result = true;
			}
		}
		return result;
	}

	// 判断输入日期跟限时日期是否同一周
	@SuppressLint("SimpleDateFormat")
	public boolean SampleWeek(String date) {
		boolean result = false;
		Date newDate = new Date();
		Date oldDate = StringToDate(date);
		Calendar newCalendar = Calendar.getInstance();
		newCalendar.setTime(newDate);
		Calendar oldCalendar = Calendar.getInstance();
		oldCalendar.setTime(oldDate);
		int subYear = newCalendar.get(Calendar.YEAR)- oldCalendar.get(Calendar.YEAR);
		if (subYear == 0) {
			if (newCalendar.get(Calendar.WEEK_OF_YEAR) == oldCalendar.get(Calendar.WEEK_OF_YEAR)) {
				result = true;
			}
		}
		return result;
	}

	// 格式化日期如yyyy-MM-dd HH:mm:ss转化为yyyyMMddHHmmss
	public static String formatDate(String date) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
					Locale.getDefault()).format(new SimpleDateFormat(
							"yyyyMMddHHmmss", Locale.getDefault()).parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
